package com.weetop.aklyscan.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.android.material.textfield.TextInputLayout;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.qmuiteam.qmui.layout.QMUIButton;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.appConfig.MyApplication;
import com.weetop.aklyscan.base.BaseActivity;
import com.weetop.aklyscan.base.mvp.BaseModel;
import com.weetop.aklyscan.presenter.LoginPresent;
import com.weetop.aklyscan.utils.BaseUtils;
import com.weetop.aklyscan.view.LoginView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cc.shinichi.library.tool.utility.ui.ToastUtil;

public class LoginActivity extends BaseActivity<LoginPresent> implements LoginView {

    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tilayoutUserName)
    TextInputLayout tilayoutUserName;
    @BindView(R.id.et_psd)
    EditText etPsd;
    @BindView(R.id.tilayoutPsd)
    TextInputLayout tilayoutPsd;
    @BindView(R.id.bt_login)
    QMUIButton btLogin;
    @BindView(R.id.order_spinner)
    TextView orderSpinner;

    String port = "1";

    @Override
    protected LoginPresent createPresenter() {
        return new LoginPresent(this, this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        //QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarLightMode(mActivity);
        String userToken = MyApplication.getUserToken();
        if (!TextUtils.isEmpty(userToken)) {
            BaseUtils.toActivity(this, MainActivity.class);
            finish();
        }
        orderSpinner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XPopup.get(LoginActivity.this).asCenterList("请选择用户类型", new String[]{"品牌商", "生产商"},
                        // null, /** 图标Id数组，可无 **/
                        // 1,    /** 选中的position，默认没有选中效果 **/
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                if (position == 0) {
                                    orderSpinner.setText("品牌商");
                                    port = "1";
                                } else if (position == 1) {
                                    orderSpinner.setText("生产商");
                                    port = "2";
                                }
                            }
                        })
                        .show();
            }
        });
    }

    @OnClick(R.id.bt_login)
    public void onViewClicked() {
        String name = etName.getText().toString().trim();
        String psd = etPsd.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            ToastUtils.showShort("用户名不能为空");
            return;
        }
        if (TextUtils.isEmpty(psd)) {
            ToastUtils.showShort("密码不能为空");
            return;
        }
        if (BaseUtils.fastClick()) {
            MyApplication.setPort(port);
            MyApplication.setUserName(name);
            mPresenter.login(name, psd, port);
        }
    }

    @Override
    public void loginSuccess(BaseModel<String> o) {
        MyApplication.setUserToken(o.getData());
        BaseUtils.toActivity(this, MainActivity.class);
        finish();
    }

    @Override
    public void showError(String msg) {
        super.showError(msg);
        ToastUtil.getInstance()._short(LoginActivity.this, msg);
    }
}
